<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="800px">
    <ContentWrap>
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="" prop="keyWord">
          <el-input placeholder="请输入内容" v-model.trim="queryParams.keyWord" />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
        <el-table-column label="是否上报" align="center" width="90">
          <template #default="scope">
            <span v-for="(item, idx) in autoReportMeterNos" :key="idx">
              <label v-if="item == scope.row.meterNo"> 是 </label>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="电表编码" align="center" prop="meterNo" width="180" />
        <el-table-column label="最后连接时间" align="center" prop="lastConnecttime" width="160">
          <template #default="scope">
            <span>{{ formatDate(scope.row.lastConnecttime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="余额" align="center" prop="lastBalance" width="120" />
        <el-table-column label="通电状态" align="center" prop="elecOpenFlag" width="90">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.METER_ELECT_OPEN_STATE" :value="scope.row.elecOpenFlag" />
          </template>
        </el-table-column>
        <el-table-column
          label="安装位置"
          align="left"
          prop="bindAddresses"
          fixed="right"
          show-overflow-tooltip
          width="200"
        >
          <template #default="scope">
            {{ scope.row.bindAddresses }}
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  </Dialog>
</template>
<script lang="ts" setup>
import { DICT_TYPE } from '@/utils/dict'
import { formatDate } from '@/utils/formatTime'
import { getMeterPage } from '@/api/zwsj/meter'

defineOptions({ name: 'MemberUserSelect' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref() // 列表的数据

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  meterSerialServerId: 0,
  keyWord: undefined
})

const autoReportMeterNos = ref([]) // 列表的数据
/** 打开弹窗 */
const open = async (serverId: number, autoReportMeterNosT: any) => {
  dialogVisible.value = true
  dialogTitle.value = '继电器下的电表列表'
  queryParams.meterSerialServerId = serverId
  autoReportMeterNos.value = autoReportMeterNosT
  getList()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const queryFormRef = ref() // 搜索的表单

/** 查询部门列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await getMeterPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

/** 初始化 **/
onMounted(async () => {
  await getList()
})
</script>
